﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace Littlefish.Validation
{
    /// <summary>
    /// A collection of ValidationError objects that is used to collect
    /// errors that occur duing calls to the Validate method.
    /// </summary>
    public class ValidationErrorCollection : List<ValidationError>
    {
        /// <summary>
        /// Adds a validation error if the condition is false. Otherwise no item is 
        /// added.
        /// <seealso>Class ValidationErrorCollection</seealso>
        /// </summary>
        /// <param name="Condition">
        /// If false the Validation Error is added.
        /// </param>
        /// <param name="Message">
        /// The Error Message for this error.
        /// </param>
        /// <returns>value of condition</returns>
        public bool Assert(bool Condition, string Message)
        {
            if (!Condition)
                this.Add(Message);

            return Condition;
        }

        /// <summary>
        /// Adds a validation error if the condition is false. Otherwise no item is 
        /// added.
        /// <seealso>Class ValidationErrorCollection</seealso>
        /// </summary>
        /// <param name="Condition">
        /// If false the Validation Error is added.
        /// </param>
        /// <param name="Message">
        /// The Error Message for this error.
        /// </param>
        /// <returns>string</returns>
        public bool Assert(bool Condition, string Message, string FieldName)
        {
            if (!Condition)
                this.Add(Message, FieldName);

            return Condition;
        }

        /// <summary>
        /// Asserts a business rule - if condition is false it's added otherwise not.
        /// <seealso>Class ValidationErrorCollection</seealso>
        /// </summary>
        /// <param name="Condition">
        /// If this condition evaluates to false the Validation Error is added
        /// </param>
        /// <param name="Error">
        /// Validation Error Object
        /// </param>
        /// <returns>value of condition</returns>
        public bool Assert(bool Condition, ValidationError Error)
        {
            if (!Condition)
                this.Add(Error);

            return Condition;
        }

        /// <summary>
        /// Returns a string representation of the errors in this collection.
        /// The string is separated by CR LF after each line.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (this.Count < 1)
                return "";

            StringBuilder sb = new StringBuilder(128);

            foreach (ValidationError Error in this)
            {
                sb.Append(Error.Message);
                sb.Append("\r\n");
            }

            return sb.ToString();
        }

        /// <summary>
        /// Returns an HTML representation of the errors in this collection.
        /// The string is returned as an HTML list.
        /// </summary>
        /// <returns></returns>
        public string ToHtml()
        {
            if (this.Count < 1)
                return "";

            StringBuilder sb = new StringBuilder(256);
            sb.Append("<ul>\r\n");

            foreach (ValidationError Error in this)
            {
                sb.Append("<li> ");

                if (Error.ControlID != null && Error.ControlID != "")
                    sb.Append("<a href='javascript:;' onclick=\"document.getElementById('" + Error.ControlID + "').focus();document.getElementById('" + Error.ControlID + "').style.background='cornsilk';\" class='errordisplay' style='border:0px;text-decoration:none'>" + Error.Message + "</a>");
                else
                    sb.Append(Error.Message);

                sb.Append("</li>\r\n");
            }

            sb.Append("</ul>\r\n");
            return sb.ToString();
        }

        public void Add(string message)
        {
            this.Add(message, "", "");
        }
        public void Add(string message, string fieldName)
        {
            this.Add(message, fieldName, "");
        }
        public void Add(string message, string fieldName, string id)
        {
            this.Add(new ValidationError(message, fieldName, id));
        }
    }
}
